using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TenantManagementService.Migrations
{
    /// <summary>
    /// 租户管理服务初始数据库结构
    /// </summary>
    public partial class Initial_Create : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "TenantManagementTenants",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    Code = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
                    Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
                    Type = table.Column<int>(type: "integer", nullable: false),
                    Status = table.Column<int>(type: "integer", nullable: false),
                    LogoUrl = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    ThemeSettings = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    ConnectionString = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    Host = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    AdminEmail = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: true),
                    AdminPhoneNumber = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
                    MaxUserCount = table.Column<int>(type: "integer", nullable: false, defaultValue: 100),
                    CurrentUserCount = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    MaxStorageSize = table.Column<long>(type: "bigint", nullable: false, defaultValue: 1024L),
                    CurrentStorageUsage = table.Column<long>(type: "bigint", nullable: false, defaultValue: 0L),
                    TrialStartTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    TrialEndTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    SubscriptionStartTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    SubscriptionEndTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    Settings = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    FeatureSwitches = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    CustomProperties = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    IsEnabled = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
                    LastAccessTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastActivationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    ExtensionCount = table.Column<int>(type: "integer", nullable: false, defaultValue: 0),
                    ExtraProperties = table.Column<string>(type: "text", nullable: true),
                    ConcurrencyStamp = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: true),
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "uuid", nullable: true),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    DeleterId = table.Column<Guid>(type: "uuid", nullable: true),
                    DeletionTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TenantManagementTenants", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "TenantManagementConfigurations",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: false),
                    Key = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
                    Value = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: false),
                    Group = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
                    Description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
                    IsEncrypted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    IsReadOnly = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    Type = table.Column<int>(type: "integer", nullable: false),
                    DefaultValue = table.Column<string>(type: "character varying(4000)", maxLength: 4000, nullable: true),
                    ValidationRules = table.Column<string>(type: "character varying(2000)", maxLength: 2000, nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
                    LastModifierId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
                    CreationTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    ExtraProperties = table.Column<string>(type: "text", nullable: true),
                    ConcurrencyStamp = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: true),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    DeleterId = table.Column<Guid>(type: "uuid", nullable: true),
                    DeletionTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_TenantManagementConfigurations", x => x.Id);
                });

            // 创建索引
            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_AdminEmail",
                table: "TenantManagementTenants",
                column: "AdminEmail");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Code",
                table: "TenantManagementTenants",
                column: "Code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_CreationTime",
                table: "TenantManagementTenants",
                column: "CreationTime");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_ExtensionCount",
                table: "TenantManagementTenants",
                column: "ExtensionCount");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Host",
                table: "TenantManagementTenants",
                column: "Host");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_IsEnabled_SubscriptionEndTime",
                table: "TenantManagementTenants",
                columns: new[] { "IsEnabled", "SubscriptionEndTime" });

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_LastAccessTime",
                table: "TenantManagementTenants",
                column: "LastAccessTime");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_LastActivationTime",
                table: "TenantManagementTenants",
                column: "LastActivationTime");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Name",
                table: "TenantManagementTenants",
                column: "Name");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Status_IsEnabled",
                table: "TenantManagementTenants",
                columns: new[] { "Status", "IsEnabled" });

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Status",
                table: "TenantManagementTenants",
                column: "Status");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_SubscriptionEndTime",
                table: "TenantManagementTenants",
                column: "SubscriptionEndTime");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_TrialEndTime",
                table: "TenantManagementTenants",
                column: "TrialEndTime");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Type_Status",
                table: "TenantManagementTenants",
                columns: new[] { "Type", "Status" });

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_Type",
                table: "TenantManagementTenants",
                column: "Type");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementTenants_IsEnabled",
                table: "TenantManagementTenants",
                column: "IsEnabled");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementConfigurations_Group",
                table: "TenantManagementConfigurations",
                column: "Group");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementConfigurations_IsEncrypted",
                table: "TenantManagementConfigurations",
                column: "IsEncrypted");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementConfigurations_IsReadOnly",
                table: "TenantManagementConfigurations",
                column: "IsReadOnly");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementConfigurations_TenantId_Key",
                table: "TenantManagementConfigurations",
                columns: new[] { "TenantId", "Key" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementConfigurations_TenantId",
                table: "TenantManagementConfigurations",
                column: "TenantId");

            migrationBuilder.CreateIndex(
                name: "IX_TenantManagementConfigurations_Type",
                table: "TenantManagementConfigurations",
                column: "Type");

            // 创建外键关系
            migrationBuilder.AddForeignKey(
                name: "FK_TenantManagementConfigurations_TenantManagementTenants_TenantId",
                table: "TenantManagementConfigurations",
                column: "TenantId",
                principalTable: "TenantManagementTenants",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_TenantManagementConfigurations_TenantManagementTenants_TenantId",
                table: "TenantManagementConfigurations");

            migrationBuilder.DropTable(
                name: "TenantManagementTenants");

            migrationBuilder.DropTable(
                name: "TenantManagementConfigurations");
        }
    }
}